package com.miyuan.wm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.miyuan.wm.mapper.AccessRecordMapper;
import com.miyuan.wm.mapper.OperationRecordMapper;
import com.miyuan.wm.pojo.AccessRecordEntity;
import com.miyuan.wm.pojo.OperationRecordEntity;
import com.miyuan.wm.request.QueryAccessRecordsRequestVo;
import com.miyuan.wm.request.QueryUserOperationLogsRequestVo;
import com.miyuan.wm.response.PageResponse;
import com.miyuan.wm.response.QueryAccessRecordsResponseVo;
import com.miyuan.wm.response.QueryUserOperationLogsResponseVo;
import com.miyuan.wm.service.SystemLogService;
import com.miyuan.wm.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName SystemLogServiceImpl
 * @Description 系统操作日志实现类
 */
@Service
@Slf4j
public class SystemLogServiceImpl implements SystemLogService {

    @Autowired
    private OperationRecordMapper operationRecordMapper;

    @Autowired
    private AccessRecordMapper accessRecordMapper;

    @Override
    public void insertAccessRecord(int userId, String userName, String accessIP, String accessType) {
        AccessRecordEntity accessRecordEntity = new AccessRecordEntity();
        accessRecordEntity.setUserId(userId);
        accessRecordEntity.setUserName(userName);
        accessRecordEntity.setAccessTime(new Date());
        accessRecordEntity.setAccessIp(accessIP);
        accessRecordEntity.setAccessType(accessType);
        accessRecordMapper.insert(accessRecordEntity);
    }

    @Override
    public void insertUserOperationRecord(int userId, String userName, String userOperationValue, String invokedResult) {
        OperationRecordEntity entity = new OperationRecordEntity();
        entity.setUserId(userId);
        entity.setUserName(userName);
        entity.setOperationName(userOperationValue);
        entity.setOperationResult(invokedResult);
        entity.setOperationTime(new Date());
        operationRecordMapper.insert(entity);
    }

    @Override
    public PageResponse<List<QueryAccessRecordsResponseVo>> listAccessRecordsLog(QueryAccessRecordsRequestVo requestVo) {
        Page<AccessRecordEntity> page = new Page<>(requestVo.getPageNumber(), requestVo.getPageSize());
        Wrapper<AccessRecordEntity> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(requestVo.getUserName())) {
            wrapper.like("user_name", requestVo.getUserName());
        }
        if (StringUtils.isNotBlank(requestVo.getStartTime())) {
            wrapper.ge("access_time", requestVo.getStartTime());
        }
        if (StringUtils.isNotBlank(requestVo.getEndTime())) {
            wrapper.lt("access_time", requestVo.getEndTime());
        }
        switch (requestVo.getType()) {
            case 1:
                wrapper.eq("access_type", SystemLogService.ACCESS_TYPE_LOGIN);
                break;
            case 2:
                wrapper.eq("access_type", SystemLogService.ACCESS_TYPE_LOGOUT);
                break;
            default:
                break;
        }
        wrapper.orderBy("access_time", false);
        List<AccessRecordEntity> accessRecordEntities = accessRecordMapper.selectPage(page, wrapper);
        List<QueryAccessRecordsResponseVo> logResponseVos = BeanUtils.convert(accessRecordEntities, QueryAccessRecordsResponseVo.class);
        return PageResponse.newInstance(logResponseVos, page.getTotal(), page.getCurrent(), page.getSize());
    }

    @Override
    public PageResponse<List<QueryUserOperationLogsResponseVo>> listUserOperationLog(QueryUserOperationLogsRequestVo requestVo) {
        Page<OperationRecordEntity> page = new Page<>(requestVo.getPageNumber(), requestVo.getPageSize());
        Wrapper<OperationRecordEntity> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(requestVo.getUserName())) {
            wrapper.like("user_name", requestVo.getUserName());
        }
        if (StringUtils.isNotBlank(requestVo.getStartTime())) {
            wrapper.ge("operation_time", requestVo.getStartTime());
        }
        if (StringUtils.isNotBlank(requestVo.getEndTime())) {
            wrapper.lt("operation_time", requestVo.getEndTime());
        }
        wrapper.orderBy("operation_time", false);
        List<OperationRecordEntity> accessRecordEntities = operationRecordMapper.selectPage(page, wrapper);
        List<QueryUserOperationLogsResponseVo> logResponseVos = BeanUtils.convert(accessRecordEntities, QueryUserOperationLogsResponseVo.class);
        return PageResponse.newInstance(logResponseVos, page.getTotal(), page.getCurrent(), page.getSize());
    }

}
